package com.gxa.medical.controller;

import com.gxa.medical.exception.BusinessException;
import com.gxa.medical.pojo.Doctor;
import com.gxa.medical.pojo.DoctorAccount;
import com.gxa.medical.pojo.User;
import com.gxa.medical.service.DoctorService;
import com.gxa.medical.service.UserDetailsService;
import com.gxa.medical.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.internal.org.objectweb.asm.tree.JumpInsnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/doctor")
@Api(value = "医生端的相关的api接口")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    //查询我的医生中的全部
    @RequestMapping("/all")
    @ApiOperation(value = "查询我的医生中的全部的方法")
    public Map selectAll() {
        JsonResult jsonResult = new JsonResult();
        List<Doctor> doctors = doctorService.selectAll();
        jsonResult.setData(doctors);
        return jsonResult.getResultMap();
    }
    @ApiOperation("医生密码的修改")
    @PostMapping("/updatePwd")
    public Map updatePwd(@RequestBody DoctorAccount doctorAccount,
                         @ApiIgnore HttpSession session,
                         @RequestParam("captcha") String captcha) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        String captcha1 = (String) session.getAttribute("captcha");
        if (captcha.equals(captcha1)){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
            Integer integer = doctorService.updatePwd(doctorAccount);
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getResultMap();
    }
    @ApiOperation("医生登录")
    @PostMapping("/longin")
    public Map loginDoctor(@RequestBody DoctorAccount doctorAccount,@ApiIgnore HttpSession session) throws BusinessException {
        String token = UUID.randomUUID().toString().replaceAll("-", " ");
        session.setAttribute("token",token);
        session.setAttribute("phone",doctorAccount.getPhone());
        List<DoctorAccount> doctorAccounts = doctorService.loginDoctor(doctorAccount);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登录成功");
        jsonResult.setData(doctorAccounts);
        jsonResult.set("token",token);

        return jsonResult.getResultMap();
    }

}
